using System;
using Godot;

namespace Arcane.Source.Story
{
    public partial class StoryView : Node, IView
    {
        [Export]
        public Control UI { get; set; }
        [Export]
        private Control _beginCombatScreen;
        [Export]
        public PackedScene dialogHandlerPrefab;

        private Control _ctrlMnt;

        public Node Node => this;

        public GameOS Os { get; set; }

        public Color? ClearColor => new Color(0x4d4d4dff);

        public void ShowCombatPrepre(Action<StringName[]> okCallback)
        {
            _beginCombatScreen.Visible = true;
            _beginCombatScreen.GetNode<Button>("Main/Right/BeginBtnMargin/BeginCombat")
                .Connect(
                    Button.SignalName.ButtonUp,
                    Callable.From(() =>
                    {
                        okCallback(["pyro", "dendro", "electro", "arcane", "tekhno"]);
                        _beginCombatScreen.Visible = false;
                    }),
                    (uint)ConnectFlags.OneShot
                );
        }

        public IDialogHandler HandleDialog()
        {
            var handler = dialogHandlerPrefab.Instantiate<DialogHandler>();
            MountControl(handler);
            return handler;
        }

        public void MountControl(Control control)
        {
            var mnt = GetControlMount();
            // Clear mnt children
            foreach (var child in mnt.GetChildren()) child.QueueFree();
            mnt.AddChild(control);
        }

        private Control GetControlMount() => _ctrlMnt ??= UI.GetNode<Control>("Mount");
    }
}